package com.gtw.common.utils;

import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.mp.WeixinProxy;
import com.foxinmy.weixin4j.mp.message.TemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 发送模板消息
 */
@Component
public class WeiXinMsgUtil {
    private static WeixinProxy weixinProxy;
    /**砍价完成通知*/
    public static final String KAN_JIA_SUCCESS_ID="Gwnr8ZKMVTVCcTlfdpY4epRGIIyG-DGEqlglAne1Hqk";
    /**佣金通知*/
    public static final String  YONG_JIN_ID="P4_Bdw91Lravq0pRpJk-hdXHyuMdtrgyu_fSVCbXigE";
    /**发货通知模版*/
    public static final String SEND_GOODS="U0WYndAYwvOPGgO0RcVtBmxy0bkQHVdIIT6dHdBwGuo";
    /**代理分红*/
    public static final String DAI_LI_FEN_HONG_ID="D6A8UxcKPFSjL5ln9f3AK8x06EAaTohAoR01zv8gow0";
    /**团队有新成员加入*/
    public static final String NEW_USER_ADD_GROUP_ID="itweOsYSvj6EXzWHYKld8_OyA0er-kSA2KwYp3B2br0";


    @Autowired
    public WeiXinMsgUtil(WeixinProxy weixinProxy){
        WeiXinMsgUtil.weixinProxy=weixinProxy;
    }


    /***
     * 砍价完成通知
     * @param goodsName
     * @param price
     */
    public static void kanjiaMsg(String goodsName, String price,String openId){
        TemplateMessage templateMessage=new TemplateMessage(openId,KAN_JIA_SUCCESS_ID,null);
        templateMessage.pushHead("您的砍价已完成。");
        templateMessage.pushItem("keyword1",goodsName);
        templateMessage.pushItem("keyword2",price);
        templateMessage.pushTail("您的砍价已完成,请及时在我的订单中支付!");
        try {
            String tmplMessage = weixinProxy.sendTmplMessage(templateMessage);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
    }


    /***
     * 发送发货通知消息
     * @param openId
     * @param goodsName
     * @param expCode
     * @param expNo
     * @param address
     */
    public static void sendDeliverMsg(String openId,String goodsName,String expCode,String expNo,String address){
        TemplateMessage templateMessage=new TemplateMessage(openId,SEND_GOODS,null);
        templateMessage.pushHead("您的订单已发货");
        templateMessage.pushItem("keyword1",goodsName);
        templateMessage.pushItem("keyword2",expCode);
        templateMessage.pushItem("keyword3",expNo);
        templateMessage.pushItem("keyword4",address);
        templateMessage.pushTail("您的订单已经由仓库交由物流，请耐心等待包裹送达！");
        try {
            String tmplMessage = weixinProxy.sendTmplMessage(templateMessage);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送佣金通知
     * @param openId
     * @param money
     */
    public static void yongjinMsg(String openId,String money){
        TemplateMessage templateMessage=new TemplateMessage(openId,YONG_JIN_ID,null);
        templateMessage.pushHead("恭喜您，获得了一笔新的佣金，您可以在“我的佣金”中查看详细佣金信息");
        templateMessage.pushItem("keyword1",money);
        templateMessage.pushItem("keyword2",DateUtils.format(new Date()));
        templateMessage.pushTail("感谢您的使用,祝您生意兴隆。");
        try {
            String tmplMessage = weixinProxy.sendTmplMessage(templateMessage);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
    }


    /**
     * 代理分红
     * @param openId
     * @param money
     */
    public static void dailifenhong(String openId,String money){
        TemplateMessage templateMessage=new TemplateMessage(openId,DAI_LI_FEN_HONG_ID,null);
        templateMessage.pushHead("有新收益了!");
        templateMessage.pushItem("keyword1",money);
        templateMessage.pushItem("keyword2",DateUtils.format(new Date()));
        templateMessage.pushTail("你的代理分红已经返送到账户,请注意查收,有疑问请联系客服");
        try {
            String tmplMessage = weixinProxy.sendTmplMessage(templateMessage);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
    }

    public static void tuanduifensi(String openId,String userId,String nickName){
        TemplateMessage templateMessage=new TemplateMessage(openId,NEW_USER_ADD_GROUP_ID,null);
        templateMessage.pushHead("恭喜您!您有新的团队粉丝加入!");
        templateMessage.pushItem("keyword1",userId);
        templateMessage.pushItem("keyword2",nickName);
        templateMessage.pushItem("keyword3",DateUtils.format(new Date()));
        templateMessage.pushTail("感谢您的支持,祝您生活愉快");
        try {
            String tmplMessage = weixinProxy.sendTmplMessage(templateMessage);
        } catch (WeixinException e) {
            e.printStackTrace();
        }
    }
}
